package com.wrx.swing.panel.generate;

import com.wrx.dto.generatesql.GenerateTotalInsertSqlByFileDto;
import com.wrx.service.generatesql.GenerateService;
import com.wrx.swing.form.SqlGenerateFormFields;
import com.wrx.util.SwingComponentUtil;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;

/**
 * SQL 生成面板
 */
public class SqlGeneratePanel {

    private final GenerateService generateService;
    private final JFrame parentFrame;

    public SqlGeneratePanel(GenerateService generateService, JFrame parentFrame) {
        this.generateService = generateService;
        this.parentFrame = parentFrame;
    }

    public JPanel createPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        // 创建表单字段管理对象
        SqlGenerateFormFields formFields = new SqlGenerateFormFields();

        // 创建输入面板
        JPanel inputPanel = createInputPanel(formFields);

        // 创建日志区域
        JScrollPane scrollPane = SwingComponentUtil.createLogArea(formFields.logArea);

        // 创建生成按钮
        JButton generateBtn = new JButton("生成SQL");

        // 绑定生成SQL事件
        setupAction(generateBtn, formFields);

        // 组装面板
        panel.add(inputPanel, BorderLayout.NORTH);
        panel.add(generateBtn, BorderLayout.CENTER);
        panel.add(scrollPane, BorderLayout.SOUTH);

        return panel;
    }

    /**
     * 创建SQL生成输入面板
     */
    private JPanel createInputPanel(SqlGenerateFormFields fields) {
        JPanel inputPanel = SwingComponentUtil.createInputPanel("参数输入", 8, 2);
        // 添加所有输入字段
        SwingComponentUtil.addLabelAndField(inputPanel, "开始点位ID:", fields.pointIdField);
        SwingComponentUtil.addLabelAndField(inputPanel, "开始设备ID:", fields.deviceIdField);
        SwingComponentUtil.addLabelAndField(inputPanel, "开始设备点位ID:", fields.devicePointIdField);
        SwingComponentUtil.addLabelAndField(inputPanel, "水厂ID:", fields.factoryIdField);
        SwingComponentUtil.addLabelAndField(inputPanel, "IoT设备ID:", fields.iotDeviceIdField);
        SwingComponentUtil.addLabelAndField(inputPanel, "租户ID:", fields.tenantIdField);
        SwingComponentUtil.addLabelAndField(inputPanel, "数据库名:", fields.dataBaseNameField);

        // 添加文件选择按钮
        JButton chooseFileBtn = new JButton("选择文件");
        inputPanel.add(chooseFileBtn);
        inputPanel.add(fields.filePathField);

        // 绑定文件选择事件
        chooseFileBtn.addActionListener(e ->
            SwingComponentUtil.openFileChooser(parentFrame, fields.filePathField, fields.logArea)
        );

        return inputPanel;
    }

    /**
     * 设置生成SQL事件
     */
    private void setupAction(JButton generateBtn, SqlGenerateFormFields fields) {
        generateBtn.addActionListener(e -> {
            try {
                // 验证文件
                File file = SwingComponentUtil.validateFile(fields.getFilePath(), fields.logArea);
                if (file == null) return;

                // 构建DTO
                GenerateTotalInsertSqlByFileDto vo = fields.buildDto();

                // 调用服务生成SQL
                String result = executeSqlGeneration(file, vo);

                // 显示结果
                SwingComponentUtil.logSuccess(fields.logArea, "SQL 生成完成！");
                fields.logArea.append(result + "\n");

                // 确认保存
                SwingComponentUtil.confirmAndSave(parentFrame, result, "generated_sql.sql", fields.logArea);

            } catch (Exception ex) {
                SwingComponentUtil.logError(fields.logArea, "执行失败", ex);
            }
        });
    }

    /**
     * 执行SQL生成
     */
    private String executeSqlGeneration(File file, GenerateTotalInsertSqlByFileDto dto) throws Exception {
        try (FileInputStream fis = new FileInputStream(file)) {
            return generateService.generateTotalInsertSqlByFile(fis, dto, null);
        }
    }
}

